/**
 * 
 */
package com.ieasy.module.system.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.beans.BeansException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ieasy.basic.dao.IBaseDao;
import com.ieasy.basic.exception.ServiceException;
import com.ieasy.basic.model.DataGrid;
import com.ieasy.basic.model.Msg;
import com.ieasy.basic.model.Pager;
import com.ieasy.basic.model.SystemContext;
import com.ieasy.basic.util.BeanUtils;
import com.ieasy.basic.util.date.DateUtils;
import com.ieasy.module.system.entity.HolidayCalEntity;
import com.ieasy.module.system.web.form.HolidayCalForm;

/**
 * @author 杨浩泉
 *
 */
@Service @Transactional
public class HolidayCalService implements IHolidayCalService {
	
	@Inject
	private IBaseDao<HolidayCalEntity> baseDao ;
	
	@Override
	public Msg add(HolidayCalForm form) {
		HolidayCalEntity entity = new HolidayCalEntity() ;
		BeanUtils.copyNotNullProperties(form, entity) ;
		entity.setCreated(new Date()) ;
		BeanUtils.copyNotNullProperties(form, entity) ;
		this.baseDao.add(entity) ;
		return new Msg(true, "添加成功！");
	}

	@Override
	public Msg delete(HolidayCalForm form) {
		try {
			if(null != form.getIds() && !"".equals(form.getIds())) {
				String[] ids = form.getIds().split(",") ;
				for (String id : ids) {
					this.baseDao.delete(HolidayCalEntity.class, id) ;
				}
				return new Msg(true, "删除成功！") ;
			}
		} catch (Exception e) {
			return new Msg(false, "删除失败！") ;
		}
		return null;
	}
	
	@Override
	public Msg update(HolidayCalForm form) {
		try {
			HolidayCalEntity entity = this.baseDao.load(HolidayCalEntity.class, form.getId()) ;
			
			if(null != entity) {	
				BeanUtils.copyNotNullProperties(form, entity) ;
				this.baseDao.update(entity);
				return new Msg(true, "编辑成功！") ;
			}
			
		} catch (BeansException e) {
			return new Msg(false, "发生错误！") ;
		}
		return new Msg(false, "编辑失败！") ;
	}
	
	@Override
	public HolidayCalForm get(String id) {
		HolidayCalEntity entity = this.baseDao.load(HolidayCalEntity.class, id) ;
		HolidayCalForm form = new HolidayCalForm() ;
		BeanUtils.copyNotNullProperties(entity, form) ;
		return form ;
	}
	
	@Override
	public List<HolidayCalForm> getYearMonth4Holiday(String yyyyMM) {
		HolidayCalForm form = new HolidayCalForm() ;
		form.setYyyyMM(yyyyMM);
		form.setType(1);
		return findList(form);
	}

	@Override
	public List<HolidayCalForm> getYearMonthDay4Holiday(String yyyyMMdd) {
		HolidayCalForm form = new HolidayCalForm() ;
		form.setYyyyMMdd(yyyyMMdd);
		form.setType(1);
		return findList(form);
	}

	@Override
	public List<HolidayCalForm> getYearMonth4Deferred(String yyyyMM) {
		HolidayCalForm form = new HolidayCalForm() ;
		form.setYyyyMM(yyyyMM);
		form.setType(2);
		return findList(form);
	}

	@Override
	public List<HolidayCalForm> getYearMonthDay4Deferred(String yyyyMMdd) {
		HolidayCalForm form = new HolidayCalForm() ;
		form.setYyyyMMdd(yyyyMMdd);
		form.setType(2);
		return findList(form);
	}
	
	@Override
	public List<HolidayCalForm> getYearMonthDayBetween4Holiday(String yyyyMMdd1, String yyyyMMdd2) {
		HolidayCalForm form = new HolidayCalForm() ;
		form.setStartDate(yyyyMMdd1);
		form.setEndDate(yyyyMMdd2);
		form.setType(1);
		return findList(form);
	}

	@Override
	public List<HolidayCalForm> getYearMonthDay4BetweenDeferred(String yyyyMMdd1, String yyyyMMdd2) {
		HolidayCalForm form = new HolidayCalForm() ;
		form.setStartDate(yyyyMMdd1);
		form.setEndDate(yyyyMMdd2);
		form.setType(2);
		return findList(form);
	}

	@Override
	public DataGrid datagrid(HolidayCalForm form) {
		if (null == form.getSort()) {
			SystemContext.setSort("t.created");
		} else {
			SystemContext.setSort("t." + form.getSort());
			SystemContext.setOrder(form.getOrder());
		}
		
		try {
			Pager<HolidayCalEntity> pager = this.find(form) ;
			List<HolidayCalForm> forms = new ArrayList<HolidayCalForm>() ;
			for (HolidayCalEntity entity : pager.getDataRows()) {
				HolidayCalForm gf = new HolidayCalForm() ;
				BeanUtils.copyNotNullProperties(entity, gf) ;
				forms.add(gf) ;
			}
			
			DataGrid dg = new DataGrid() ;
			dg.setTotal(pager.getTotal());
			dg.setRows(forms);
			return dg;
		} catch (Exception e) {
			throw new ServiceException("加载信息异常", e) ;
		}
	}

	
	private Pager<HolidayCalEntity> find(HolidayCalForm form) {
		Map<String, Object> alias = new HashMap<String, Object>();
		String hql = "select t from HolidayCalEntity t where 1=1 " ;
		hql = addWhere(hql, form, alias) ;
		return this.baseDao.find(hql, alias) ;
	}

	private List<HolidayCalForm> findList(HolidayCalForm form) {
		List<HolidayCalForm> forms = new ArrayList<HolidayCalForm>() ;
		
		Map<String, Object> alias = new HashMap<String, Object>();
		String hql = "select t from HolidayCalEntity t where 1=1 " ;
		hql = addWhere(hql, form, alias) ;
		List<HolidayCalEntity> list = this.baseDao.list(hql, alias) ;
		
		for (HolidayCalEntity entity : list) {
			HolidayCalForm gf = new HolidayCalForm() ;
			BeanUtils.copyNotNullProperties(entity, gf) ;
			forms.add(gf) ;
		}
		return forms ;
	}

	private String addWhere(String hql, HolidayCalForm form, Map<String, Object> params) {
		if (form.getType() != null && !"".equals(form.getType())) {
			hql += " and t.type=:type";
			params.put("type", form.getType());
		}
		if (form.getTitle() != null && !"".equals(form.getTitle())) {
			hql += " and t.title=:title";
			params.put("title", form.getTitle());
		}
		if (form.getYyyy() != null && !"".equals(form.getYyyy())) {
			hql += " and date_format(t.holidayDate,'%Y')=:year";
			params.put("year", form.getYyyy());
		}
		if (form.getYyyyMM() != null && !"".equals(form.getYyyyMM())) {
			hql += " and date_format(t.holidayDate,'%Y-%m')=:month";
			params.put("month", form.getYyyyMM());
		}
		if (form.getYyyyMMdd() != null && !"".equals(form.getYyyyMMdd())) {
			hql += " and date_format(t.holidayDate,'%Y-%m-%d')>=:day and date_format(t.holidayDate,'%Y-%m')=:month";
			params.put("day", form.getYyyyMMdd());
			params.put("month", DateUtils.dateToString(DateUtils.formatYYYYMMDD(form.getYyyyMMdd()), "yyyy-MM"));
		}
		if (form.getStartDate() != null && !"".equals(form.getStartDate()) && form.getEndDate() != null && !"".equals(form.getEndDate())) {
			hql += " and date_format(t.holidayDate,'%Y-%m-%d')>=:sd and date_format(t.holidayDate,'%Y-%m-%d')<=:ed";
			params.put("sd", form.getStartDate());
			params.put("ed", form.getEndDate());
		}
		if (form.getHolidayDate() != null && !"".equals(form.getHolidayDate())) {
			hql += " and date_format(t.holidayDate,'%Y-%m-%d')=:holidayDate";
			params.put("holidayDate", form.getHolidayDate());
		}
		return hql;
	}

	

}
